package kr.makjang.lib.net;

import java.util.List;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

/**
 * @brif 네트워크 정보를 확인하고 설정하는 클래스
 * 이 클래스를 사용하기 위해선
 * 퍼미션을 줘야 한다.
 * <pre>
 * @permission android.permission.ACCESS_NETWORK_STATE
 * android.permission.CHANGE_WIFI_STATE
 * android.permission.ACCESS_WIFI_STATE
 * android.permission.INTERNET
 * </pre>
 * @author Claude.H
 * @since 2011/12/20
 *
 */
public class MakNetwork
{
    public final static int TYPE_MOBILE = 0;

    public final static int TYPE_WIFI = 1;

    public final static int TYPE_NULL = 2;
        
    private Context mContext;
    
    public MakNetwork(Context context)
    {
        mContext = context;
    }
    
    /**
     * 현재 네트워크가 Mobile 인지 Wifi 인지 확인
     * @return
     */
    @SuppressWarnings("static-access")
    public int getNetworkInfo()
    {
        ConnectivityManager conn;
        NetworkInfo info;
        conn = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
        info = conn.getActiveNetworkInfo();
        
        int result = TYPE_NULL;
        switch(info.getType())
        {
            case TYPE_MOBILE:
                result = TYPE_MOBILE;
                break;
            case TYPE_WIFI:
                result = TYPE_WIFI;
                break;
        }
        
        return result;
    }
    
    /**
     * 현재 Mobile에 연결 되어 있는지 체크
     * @return boolean
     */
    @SuppressWarnings("static-access")
    public boolean isMobile()
    {
        ConnectivityManager conn;
        conn = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
        
        return conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    }
    
    /**
     * 현재 Wifi에 연결이 되어 있는지 체크
     * @return boolean
     */
    @SuppressWarnings("static-access")
    public boolean isWifi()
    {
        ConnectivityManager conn;
        conn = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
        
        return conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    }
    
    /**
     * 현재 Mobile, Wifi 둘중에 하나라도 연결이 되어 있는지 체크
     * @return boolean
     */
    @SuppressWarnings("static-access")
    public boolean isNetwork()
    {
        ConnectivityManager conn;
        conn = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
        
        boolean isMobile = conn.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
        boolean isWifi   = conn.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
        
        if (isMobile == true || isWifi == true)
            return true;
        
        return false;
    }
    
    /**
     * 파라미터의 타입으로 연결이 되어 있는지 체크
     * 
     * <table>
     *     <tr>
     *          <td><font face="Arial"><b>Summary</b></font></td>
     *          <td><font face="Arial"><b>Constants</b></font></td>
     *     </tr>
     *     <tr>
     *          <td><i><b>TYPE_MOBILE</b></i></td>
     *          <td>mobile network type</td>
     *     </tr>
     *     <tr>
     *          <td><i><b>TYPE_WIFI</b></i></td>
     *          <td>wifi network type</td>
     *     </tr>
     * </table>
     * @return boolean
     */
    @SuppressWarnings("static-access")
    public boolean isNetwork(int type)
    {
        ConnectivityManager conn;
        conn = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
        
        return conn.getNetworkInfo(type).isConnected();
    }
    
    /**
     * WIFI에 접속 되었다면 어떤 WIFI SSID를 넘겨 준다.
     * @return
     */
    @SuppressWarnings("static-access")
    public String getWifiInfo()
    {
        WifiManager wifiManager = (WifiManager) mContext.getSystemService(mContext.WIFI_SERVICE);
        return wifiManager.getConnectionInfo().getSSID();
    }
    
    /**
     * Wifi를 검색해서 넘겨준다.
     * @return
     */
    @SuppressWarnings("static-access")
    public List<WifiConfiguration> startScan()
    {
        WifiManager wifiManager = (WifiManager) mContext.getSystemService(mContext.WIFI_SERVICE);
        return wifiManager.getConfiguredNetworks();
    }
}
